import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:iea/provider/base/base_resp.dart';
import 'package:iea/blocs/indexPage_blocs/index_openCourse_bloc.dart';
import 'package:iea/models/indexPage_models/index_openCourse_model.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:iea/widgets/loading.dart';
class Indexbanner extends StatefulWidget{
  @override 
  State<StatefulWidget> createState() => _IndexbannerState();
}
class _IndexbannerState extends State<Indexbanner>
  with AutomaticKeepAliveClientMixin<Indexbanner> {
  @override
  bool get wantKeepAlive => true;
  GetIndexBannerListBloc _bloc = new GetIndexBannerListBloc();
  List<IndexOpenCourseModel> indexBannerList = [];
   _getIndexBanner() {
    Map<String, String>  params = {'type': '1'};
    _bloc.getIndexBannerList(params);
  }
  @override
  void initState() {
    super.initState();
    _getIndexBanner();
  }
  @override 
  Widget build(BuildContext context){
    super.build(context);
    ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
    return StreamBuilder<BaseResp>(
      stream: _bloc.indexBannerStream,
      builder: (context, AsyncSnapshot<BaseResp> snapshot) {
       if (snapshot.data != null && snapshot.hasData) {
          var response = snapshot.data;
          if (response.result == true && response.data != null && response.data.length > 0) {
            indexBannerList = response.data;
            if (indexBannerList.length > 1) {
              return Container(
                height: ScreenUtil().setHeight(122),
                child: Swiper(
                  itemBuilder: (BuildContext context,int index){
                    return Padding(
                      padding: EdgeInsets.symmetric(horizontal: 17.0),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(10),
                        child: FadeInImage.memoryNetwork(
                          placeholder: kTransparentImage,
                          image: indexBannerList[index].picture,
                          fit: BoxFit.fill
                        ),
                      ),
                    );
                  },
                  itemCount: indexBannerList.length,
                  autoplay: true,
                  duration: 300,
                  // autoplayDelay: 3000,
                  pagination: SwiperPagination(
                    margin: EdgeInsets.only(bottom: 10.0),
                    builder: DotSwiperPaginationBuilder(
                      color: Colors.blue[600],
                      activeColor: Colors.white,
                      space: 5,
                    ),
                  ),
                  onTap: (index) {
                    debugPrint("点击了第:$index个");
                  },
                  // itemWidth: 375.0,
                  // itemHeight: 122.0,
                  // scrollDirection: Axis.vertical,
                  // control: new SwiperControl(),
                ),
              );
            } else {
              return GestureDetector(
                onTap: () {
                  print('banner');
                },
                child: Padding(
                  padding: EdgeInsets.symmetric(horizontal: 17.0),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(10.0),
                    child: FadeInImage.memoryNetwork(
                      placeholder: kTransparentImage,
                      image: indexBannerList[0].picture,
                      fit: BoxFit.fill,
                      height: ScreenUtil().setHeight(122),
                    ),
                  ),
                ),
              );
            }
          } else {
            return Container();
          }
        } else {
          return LoadingPage();
        }
      }
    );
  }
}